import http from '../utils/request.js'
import {
	sm2
} from 'sm-crypto'

const publicKey =
	'040a302b5e4b961afb3908a4ae191266ac5866be100fc52e3b8dba9707c8620e64ae790ceffc3bfbf262dc098d293dd3e303356cb91b54861c767997799d2f0060'
const cipherMode = 1 // C1C3C2

const encryptPassword = (password) => {
	console.log(publicKey, cipherMode)
	return '04' + sm2.doEncrypt(password, publicKey, cipherMode)
}

const loginApi = {
	// 用户登录 (JSON 格式)
	login(data) {
		const encryptedPassword = encryptPassword(data.password)
		return http.post('/member/login', {
			username: data.username,
			password: encryptedPassword
		}, {
			isFormData: false // 明确指定 JSON 格式
		})
	},
	// 会员注册 (form-data 格式)
	register(data) {
		return http.post('/member/register', {
			password: data.password,
			telephone: data.telephone,
			authCode: data.authCode,
			email: data.email,
			referrerCode: data.referrerCode
		}, {
			isFormData: true // 明确指定 form-data 格式
		})
	},
	// 发送邮箱验证码
	sendEmailcode(data) {
		return http.post('/member/send/emailcode', data, {
			isFormData: true
		})
	},
	// 退出登录 (默认 JSON 格式)
	logout() {
		return http.post('/logout', {}, {
			isFormData: false // 明确指定 JSON 格式
		})
	}
}

export default loginApi